import config from './config.js';
import store from '../store/index.js';
export default {
	request(options = {}) {
		return new Promise((resolve, reject) => {
			let url = options.url;
			if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
				options.url = config.domain + url;
			}

			
			//判断本地是否有用户信息
			var token = uni.getStorageSync("token");
			if (token) {
				//将token放在请求头中（authorization）
				options.header.token=token;
			}
			

			 
		
			
			//调用这个方法后就发送请求了
			options.complete = (response) => { //回调函数
				if (response.statusCode == 200) { //http状态
					let code = response.data.code; //返回的状态码

					if (code == 500) {  //code=500服务器端报错
						uni.showToast({  //将错误信息展示给用户（）
							title: response.data.msg,
							icon: "none"
						});
					}

					resolve(response.data)
				} else if (response.statusCode == 420) {
					uni.navigateTo({
						url: "/pages/user/login"
					})
				}
			}

			uni.request(options)
		})
	},

	post(url, data = {}, header = {}) {
		 

		let options = {
			url: url,
			data: data,
			header: header,
			method: "POST"
		}

		return this.request(options);
	},

	get(url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header
		}

		return this.request(options);
	}
};
